package base;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class DataConverter {
    public static final Map<Class<?>, Function<String, ?>> CONVERTERS = new HashMap<>();
    static {
        CONVERTERS.put(Integer.class, Integer::valueOf);
        CONVERTERS.put(int.class, Integer::valueOf);
        CONVERTERS.put(Double.class, Double::valueOf);
        CONVERTERS.put(double.class, Double::valueOf);
        CONVERTERS.put(Long.class, Long::valueOf);
        CONVERTERS.put(long.class, Long::valueOf);
        CONVERTERS.put(Boolean.class, Boolean::valueOf);
        CONVERTERS.put(boolean.class, Boolean::valueOf);
        CONVERTERS.put(Float.class, Float::valueOf);
        CONVERTERS.put(float.class, Float::valueOf);
        CONVERTERS.put(Byte.class, Byte::valueOf);
        CONVERTERS.put(byte.class, Byte::valueOf);
        CONVERTERS.put(Short.class, Short::valueOf);
        CONVERTERS.put(short.class, Short::valueOf);
        CONVERTERS.put(String.class, s -> s);
        CONVERTERS.put(Character.class, s -> {
            if (s == null || s.isEmpty()) {
                throw new IllegalArgumentException("Cannot convert empty string to Character");
            }
            return Character.valueOf(s.charAt(0));
        });
        CONVERTERS.put(char.class, s -> {
            if (s == null || s.isEmpty()) {
                throw new IllegalArgumentException("Cannot convert empty string to char");
            }
            return s.charAt(0);
        });
    }
}
